#pragma once

#include <QDialog>
#include <QEventLoop>
#include <QTreeWidgetItem>
#include <QWidget>
#include <QXmlStreamReader>

QT_BEGIN_NAMESPACE
namespace Ui {
  class Dialog;
}
QT_END_NAMESPACE

class Dialog : public QDialog {
  Q_OBJECT

 public:
  Dialog(QWidget* parent = nullptr);
  ~Dialog();
  void load();
  void saveAs();
  QStringList getItemText();


 private:
  bool read(QIODevice* device);
  bool write(QIODevice* device);

  void readKeMu();
  void readItem(QTreeWidgetItem* item);
  void readCode(QTreeWidgetItem* item);
  void readName(QTreeWidgetItem* item);
  void readType(QTreeWidgetItem* item);
  void readOrient(QTreeWidgetItem* item);

  void onItemDoubleClicked(QTreeWidgetItem* item);

  void writeItem(const QTreeWidgetItem* item);

  QTreeWidgetItem* createChildItem(QTreeWidgetItem* item);

  void setSelectionMode(QTreeWidget *w);
  void setDragAndDrop(QTreeWidget *w,bool dd=true);

 signals:

 protected:

 private slots:

  void on_btnXMLSave_clicked();

  void on_btnItemModify_clicked();

  void on_btnXMLLoad_clicked();

  void setSignSlots(QTreeWidget *w);

  void on_btnOK_clicked();

  void setButtonState();

  void on_btnItemAdd_clicked();

  void on_btnItemDel_clicked();

 private:
  Ui::Dialog* ui;
  QXmlStreamReader xml;
  QXmlStreamWriter xmlw;
  QList<QTreeWidget*> trees;
  QStringList list;
  QEventLoop loop;
  bool  canBeModified = false;
};
